package cn.imnaer.database;

import java.util.List;

import org.bson.BSONObject;

import com.sequoiadb.base.CollectionSpace;
import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.Sequoiadb;

import cn.imnaer.database.nosql.DbConnectionPool;

public class DBAccessAPIHelp extends SequoiaDataAccess {

	public BSONObject queryOne(String dbSpaceName, String dbCollectionName, BSONObject matcher, BSONObject selector,
			BSONObject order) {

		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.queryOne(dbCollection, matcher, selector, order);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;
	}

	public List<BSONObject> queryAll(String dbSpaceName, String dbCollectionName) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.queryAll(dbCollection);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;
	}

	public List<BSONObject> queryAll(String dbSpaceName, String dbCollectionName, BSONObject matcher,
			BSONObject selector, BSONObject orderBy) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.queryAll(dbCollection, matcher, selector, orderBy);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;
	}

	public List<BSONObject> queryAll(String dbSpaceName, String dbCollectionName, BSONObject matcher, BSONObject orderBy) {

		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.queryAll(dbCollection, matcher, null, orderBy);
		} catch (Exception e) {
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;
	}

	public List<BSONObject> queryAll(String dbSpaceName, String dbCollectionName, BSONObject matcher,
			BSONObject selector, BSONObject orderBy, long skipRows, long returnRows) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.queryAll(dbCollection, matcher, selector, orderBy, skipRows, returnRows);

		} catch (Exception e) {
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;
	}

	public List<BSONObject> aggregate(String dbSpaceName, String dbCollectionName, BSONObject match, BSONObject group,
			BSONObject project) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return this.aggregate(dbCollection, match, group, project);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
		return null;

	}

	public void update(String dbSpaceName, String dbCollectionName, BSONObject matcher, Object obj) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			super.update(dbCollection, matcher, obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
	}

	public void userDefinedUpdate(String dbSpaceName, String dbCollectionName, BSONObject matcher, Object modifier) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			try {
				super.userDefinedUpdate(dbCollection, matcher, modifier);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
		} finally {
			DbConnectionPool.free(sdb);
		}
	}

	public void insert(String dbSpaceName, String dbCollectionName, Object obj) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			this.insert(dbCollection, obj);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
	}

	public void delete(String dbSpaceName, String dbCollectionName, BSONObject matcher) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			this.delete(dbCollection, matcher);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
	}

	public long getCount(String dbSpaceName, String dbCollectionName, BSONObject matcher) {

		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			return dbCollection.getCount(matcher);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
		return 0;
	}

	public void batchInsert(String dbSpaceName, String dbCollectionName, List<BSONObject> items) {
		Sequoiadb sdb = null;
		try {
			sdb = DbConnectionPool.getConnection();
			DBCollection dbCollection = getDbCollection(dbSpaceName, dbCollectionName, sdb);
			this.batchInsert(dbCollection, items);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionPool.free(sdb);
		}
	}

	private DBCollection getDbCollection(String dbSpaceName, String dbCollectionName, Sequoiadb sdb) {
		CollectionSpace db = null;
		DBCollection dbCollection = null;
		if (sdb.isCollectionSpaceExist(dbSpaceName))
			db = sdb.getCollectionSpace(dbSpaceName);
		else
			db = sdb.createCollectionSpace(dbSpaceName);

		if (db.isCollectionExist(dbCollectionName))
			dbCollection = db.getCollection(dbCollectionName);
		else
			dbCollection = db.createCollection(dbCollectionName);
		return dbCollection;
	}

}
